//
//  UINavigationBar+Transparency.m
//  JiaolianClient
//
//  Created by hlj on 15/9/12.
//  Copyright (c) 2015年 NaoDong. All rights reserved.
//

#import "UINavigationBar+Transparency.h"
#import "UIColor+HLJColor.h"
#import "UIImage+HLJImage.h"
@implementation UINavigationBar (Transparency)


#pragma mark - private
//设置状态栏透明
-(void)setNavigationBarTransparent:(BOOL)transparent
{
    if (transparent) {
        [self setBarTintColor:[UIColor clearColor]];
        UIImage * clearImage = [UIImage imageWithFillColor:[UIColor clearColor] Size:CGSizeMake(1, 1)];
        [self setBackgroundImage:clearImage forBarMetrics:UIBarMetricsDefault];
        self.translucent = YES;
        [self setShadowImage:clearImage];
    }else
    {
        UIImage * backgrounImage = [UIImage imageWithFillColor:[UIColor navColor]  Size:CGSizeMake(1, 1)];

        [self setBackgroundImage:backgrounImage forBarMetrics:UIBarMetricsDefault];

//        UIImage * shadowImage = [UIImage imageWithFillColor:ColorWithHexString(@"e5e5e5")  Size:CGSizeMake(1, 0.5)];//[UIImage imageWithColor:[UIColor colorFromHexString:@"091019"] cornerRadius:0];
        UIImage *shadowImage = Image(@"nav_shadow");

        [self setShadowImage:shadowImage];
 //
    }
    [self setHidden:transparent];
}
-(void)setNavigationBarTransparentWithLineAndColor:(UIColor *)color
{
   
    UIImage * clearImage = [UIImage imageWithFillColor:color Size:CGSizeMake(1, 1)];
    [self setBackgroundImage:clearImage forBarMetrics:UIBarMetricsDefault];
    self.translucent = YES;
    [self setShadowImage:[UIImage imageWithFillColor:color Size:CGSizeMake(1, 0.5)]];

}

-(void)setNavigationBarNoLine
{
    
    UIImage * backgrounImage = [UIImage imageWithFillColor:[UIColor navColor]  Size:CGSizeMake(1, 1)];
    
    [self setBackgroundImage:backgrounImage forBarMetrics:UIBarMetricsDefault];
    
    UIImage * shadowImage = [UIImage imageWithFillColor:[UIColor clearColor]  Size:CGSizeMake(1, 0.5)];//[UIImage imageWithColor:[UIColor colorFromHexString:@"091019"] cornerRadius:0];
    
    [self setShadowImage:shadowImage];
    //
}

static char overlayKey;

- (UIView *)overlay
{
    return objc_getAssociatedObject(self, &overlayKey);
}

- (void)setOverlay:(UIView *)overlay
{
    objc_setAssociatedObject(self, &overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)lt_setBackgroundColor:(UIColor *)backgroundColor
{
    if (!self.overlay) {
        [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, CGRectGetHeight(self.bounds) + 20)];
        self.overlay.userInteractionEnabled = NO;
        self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self insertSubview:self.overlay atIndex:0];
    }
    self.overlay.backgroundColor = backgroundColor;
}

- (void)lt_setTranslationY:(CGFloat)translationY
{
    self.transform = CGAffineTransformMakeTranslation(0, translationY);
}

- (void)lt_setElementsAlpha:(CGFloat)alpha
{
    [[self valueForKey:@"_leftViews"] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger i, BOOL *stop) {
        view.alpha = alpha;
    }];
    
    [[self valueForKey:@"_rightViews"] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger i, BOOL *stop) {
        view.alpha = alpha;
    }];
    
    UIView *titleView = [self valueForKey:@"_titleView"];
    titleView.alpha = alpha;
}

- (void)lt_reset
{
    [self setBackgroundImage:[UIImage imageNamed:@"top-bg"] forBarMetrics:UIBarMetricsDefault];
    [self.overlay removeFromSuperview];
    self.overlay = nil;
}
@end
